package com.team.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.team.entity.Shoper;
import com.team.service.Impl.ShoperServiceImpl;

@Controller
public class ShoperController {
	  @Resource
	  private ShoperServiceImpl  shoperServiceImpl;
	  
	  @RequestMapping(value="add",method = RequestMethod.POST,produces = "text/plain;charset=UTF-8")
    public String add(Shoper shoper,@RequestParam("file1") MultipartFile file1,HttpServletRequest request)throws IllegalStateException, IOException{
		  System.out.println(shoper);
		  if(!file1.isEmpty()){			  
			  file1.transferTo(new File(request.getServletContext().getRealPath("img"),file1.getOriginalFilename()));
			  shoper.setImg(file1.getOriginalFilename());
		  }  
		  
		  boolean result = shoperServiceImpl.add(shoper);
		  return "foundAll";
		  }
	  
	  
	  @RequestMapping("detele")
	  public String detele(String id) {
		  int ids=Integer.parseInt(id);
		  shoperServiceImpl.detele(ids);
		     return "foundAll";
	}

	  @RequestMapping(value="update")
	  public  String update(Shoper shoper){
	
		  shoperServiceImpl.update(shoper);
		  return "foundAll";
	  
	  }

	  @RequestMapping("foundAll")
	  public String foundAll(Model model){
		 List<Shoper> list = shoperServiceImpl.foundAll();
	 	 //Model方式数据回显，将数据带到jsp
		 model.addAttribute("lists",list);
		  return "index.jsp";
		
}
	  }
